package com.gx.smart.building.ui.maintain.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import androidx.activity.result.ActivityResultLauncher;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.gx.smart.base.BaseAdapter;
import com.gx.smart.building.R;
import com.gx.smart.building.databinding.ItemMaintainResultBinding;
import com.gx.smart.building.ui.common.adapter.UploadImageUtil;
import com.gx.smart.building.ui.maintain.viewmodel.MaintainViewModel;
import com.gx.smart.lib_http.BuildConfig;
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.dto.PlanTaskAssetItemResultDto;
import com.mylhyl.circledialog.CircleDialog;

import java.util.List;

/**
 * @author xiaosy
 * @create 2020/5/8
 * @Describe 维修详情
 **/
public class MaintainResultAdapter extends BaseAdapter<PlanTaskAssetItemResultDto, MaintainResultAdapter.ViewHolder> {
    private final int handleStatus;
    private final ActivityResultLauncher<Intent> photoLauncher;
    private final ActivityResultLauncher<Intent> cameraLauncher;
    public UploadImageUtil[] uploadImageUtil;
    private final MaintainViewModel viewModel;
    public int selectedPosition;
    private final List<PlanTaskAssetItemResultDto.Builder> planTaskAssetItemResultDto;

    public MaintainResultAdapter(Context context, int handleStatus,
                                 ActivityResultLauncher<Intent> photoLauncher,
                                 ActivityResultLauncher<Intent> cameraLauncher,
                                 MaintainViewModel viewModel,
                                 List<PlanTaskAssetItemResultDto.Builder> planTaskAssetItemResultDto) {

        super(context);
        this.handleStatus = handleStatus;
        this.photoLauncher = photoLauncher;
        this.cameraLauncher = cameraLauncher;
        this.viewModel = viewModel;
        this.planTaskAssetItemResultDto = planTaskAssetItemResultDto;
    }

    @Override
    protected int onBindLayout() {
        return R.layout.item_maintain_result;
    }

    @Override
    public void addAll(List<PlanTaskAssetItemResultDto> list) {
        super.addAll(list);
        this.uploadImageUtil = new UploadImageUtil[list.size()];
    }

    @Override
    public void refresh(List<PlanTaskAssetItemResultDto> list) {
        super.refresh(list);
        this.uploadImageUtil = new UploadImageUtil[list.size()];
    }

    @Override
    protected ViewHolder onCreateHolder(View view) {
        ItemMaintainResultBinding binding = ItemMaintainResultBinding.inflate(LayoutInflater.from(mContext),
                (ViewGroup) view, false);
        return new ViewHolder(binding);
    }

    @Override
    protected void onBindData(ViewHolder holder, PlanTaskAssetItemResultDto item, int position) {
        holder.binding.title.setText(item.getItemName());
        //结果类型 1-是否，2-数值
        if (item.getItemResultType() == 1) {
            holder.binding.radioGroup.setVisibility(View.VISIBLE);
            holder.binding.value.setVisibility(View.GONE);
            holder.binding.resultType.setVisibility(View.GONE);
        } else if (item.getItemResultType() == 2) {
            holder.binding.radioGroup.setVisibility(View.GONE);
            holder.binding.value.setVisibility(View.VISIBLE);
            holder.binding.resultType.setVisibility(View.GONE);
        } else if (item.getItemResultType() == 3) {
            holder.binding.radioGroup.setVisibility(View.GONE);
            holder.binding.value.setVisibility(View.GONE);
            holder.binding.resultType.setVisibility(View.VISIBLE);
        }
        //是否需要图片：1-是，2否
        if (item.getImageRequire() == 1) {
            holder.binding.uploadImage.setVisibility(View.VISIBLE);
        } else {
            holder.binding.uploadImage.setVisibility(View.GONE);
        }

        holder.binding.title.setText(item.getItemName());
        holder.binding.title.setOnClickListener((view) -> {
            new CircleDialog.Builder()
                    .setTitle("项目内容")
                    .setText(item.getItemContent())
                    .setPositive("确定", null)
                    .show(((AppCompatActivity) mContext).getSupportFragmentManager());
        });
        //状态：1-已处理，2-未处理
        if (handleStatus == 1) {
            holder.binding.radioGroup.setEnabled(false);
            if (item.getItemResultType() == 1) {
                if (item.getResultValue().equals("1")) {
                    holder.binding.yes.setChecked(true);
                } else {
                    holder.binding.no.setChecked(true);
                }
            } else if (item.getItemResultType() == 2) {
                holder.binding.value.setText(item.getResultValue());
            } else if (item.getItemResultType() == 3) {
                //完成
                if (item.getResultValue().equals("1")) {
                    holder.binding.resultType.setText("已完成");
                } else {
                    holder.binding.resultType.setText("完成");
                }

            }
            holder.binding.yes.setEnabled(false);
            holder.binding.no.setEnabled(false);
            holder.binding.value.setEnabled(false);
            holder.binding.uploadImage.setOnClickListener(null);
            holder.binding.addImage.setVisibility(View.GONE);
            if (item.getTaskAssetItemFileDtoCount() > 0) {
                String fileUrl = item.getTaskAssetItemFileDto(0).getFileUrl();
                Glide.with(mContext).load(BuildConfig.FILE_UPLOAD_URL + fileUrl).into(holder.binding.idItemAttachmentImage);
            }
            holder.binding.idItemAttachmentDelete.setVisibility(View.GONE);
            holder.binding.resultType.setEnabled(false);
        } else {
            planTaskAssetItemResultDto.get(position).setResultValue("2");
            holder.binding.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    switch (checkedId) {
                        case R.id.yes:
                            planTaskAssetItemResultDto.get(position).setResultValue("1");
                            break;
                        case R.id.no:
                            planTaskAssetItemResultDto.get(position).setResultValue("2");
                            break;
                    }
                }
            });

            holder.binding.value.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    String value = s.toString().trim();
                    planTaskAssetItemResultDto.get(position).setResultValue(value);
                }
            });
            //3-检查步骤（结果填写1-已完成，2-未完成，默认2-未完成）
            holder.binding.resultType.setOnClickListener(v -> {
                planTaskAssetItemResultDto.get(position).setResultValue("1");
                holder.binding.resultType.setText("已完成");
                holder.binding.resultType.setEnabled(false);
            });
            uploadImageUtil[position] = new UploadImageUtil(mContext, photoLauncher, cameraLauncher);
            holder.binding.radioGroup.setEnabled(true);
            holder.binding.yes.setEnabled(true);
            holder.binding.no.setEnabled(true);
            holder.binding.value.setEnabled(true);
            holder.binding.addImage.setVisibility(View.VISIBLE);
            holder.binding.idItemAttachmentDelete.setVisibility(View.GONE);
            holder.binding.uploadImage.setOnClickListener((view) -> {
                selectedPosition = position;
                uploadImageUtil[position].setListener(new UploadImageUtil.UploadImageListener() {
                    @Override
                    public void displayImage1(Uri uri) {
                        holder.binding.addImage.setVisibility(View.GONE);
                        holder.binding.idItemAttachmentImage.setVisibility(View.VISIBLE);
                        holder.binding.idItemAttachmentDelete.setVisibility(View.VISIBLE);
                        Glide.with(mContext).load(uri).into(holder.binding.idItemAttachmentImage);
                    }

                    @Override
                    public void displayFail() {
                        holder.binding.idTvRefresh.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void deleteImage() {
                        holder.binding.addImage.setVisibility(View.VISIBLE);
                        holder.binding.idItemAttachmentImage.setVisibility(View.GONE);
                        holder.binding.idItemAttachmentDelete.setVisibility(View.GONE);
                        planTaskAssetItemResultDto.get(position).removeTaskAssetItemFileDto(0);
                    }

                    @Override
                    public void uploadTask1(String fileName, byte[] file) {
                        viewModel.uploadAdapterImage(fileName, file);
                    }
                });

                if (planTaskAssetItemResultDto.get(position).getTaskAssetItemFileDtoCount() > 0
                ) {
                    uploadImageUtil[position].deleteImage1();
                } else {
                    uploadImageUtil[position].uploadImage1();
                }

            });
        }
    }

    protected static class ViewHolder extends RecyclerView.ViewHolder {
        private final ItemMaintainResultBinding binding;

        public ViewHolder(ItemMaintainResultBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
